home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / tools / ras2tiff.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  6KB  |  185 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/ras2tiff.c,v 1.8 92/02/10 19:04:19 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include "rasterfile.h"
  31. #include "tiffio.h"
  32.  
  33. typedef    unsigned char u_char;
  34. typedef    unsigned short u_short;
  35. typedef    unsigned long u_long;
  36.  
  37. #define    howmany(x, y)    (((x)+((y)-1))/(y))
  38. #define    streq(a,b)    (strcmp(a,b) == 0)
  39.  
  40. u_short    config = PLANARCONFIG_CONTIG;
  41. u_short    compression = -1;
  42. u_long    rowsperstrip = (u_long) -1;
  43.  
  44. main(argc, argv)
  45.     char *argv[];
  46. {
  47.     u_char *buf;
  48.     int row, linebytes;
  49.     TIFF *out;
  50.     FILE *in;
  51.     struct rasterfile h;
  52.  
  53.     argc--, argv++;
  54.     if (argc < 2)
  55.         usage();
  56.     for (; argc > 2 && argv[0][0] == '-'; argc--, argv++) {
  57.         if (streq(argv[0], "-none")) {
  58.             compression = COMPRESSION_NONE;
  59.             continue;
  60.         }
  61.         if (streq(argv[0], "-packbits")) {
  62.             compression = COMPRESSION_PACKBITS;
  63.             continue;
  64.         }
  65.         if (streq(argv[0], "-lzw")) {
  66.             compression = COMPRESSION_LZW;
  67.             continue;
  68.         }
  69.         if (streq(argv[0], "-rowsperstrip")) {
  70.             argc--, argv++;
  71.             rowsperstrip = atoi(argv[0]);
  72.             continue;
  73.         }
  74.         usage();
  75.     }
  76.     in = fopen(argv[0], "r");
  77.     if (in == NULL) {
  78.         fprintf(stderr, "%s: Can not open.\n", argv[0]);
  79.         exit(-1);
  80.     }
  81.     if (fread(&h, sizeof (h), 1, in) != 1) {
  82.         fprintf(stderr, "%s: Can not read header.\n", argv[0]);
  83.         exit(-2);
  84.     }
  85.     if (h.ras_magic != RAS_MAGIC) {
  86.         fprintf(stderr, "%s: Not a rasterfile.\n", argv[0]);
  87.         exit(-3);
  88.     }
  89.     out = TIFFOpen(argv[1], "w");
  90.     if (out == NULL)
  91.         exit(-4);
  92.     TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (u_long) h.ras_width);
  93.     TIFFSetField(out, TIFFTAG_IMAGELENGTH, (u_long) h.ras_height);
  94.     TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  95.     TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, h.ras_depth > 8 ? 3 : 1);
  96.     TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, h.ras_depth > 1 ? 8 : 1);
  97.     TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
  98.     if (h.ras_maptype != RMT_NONE) {
  99.         register u_short *red;
  100.         register int i, j;
  101.         int mapsize;
  102.  
  103.         buf = (u_char *)malloc(h.ras_maplength);
  104.         if (buf == NULL) {
  105.             fprintf(stderr, "No space to read in colormap.\n");
  106.             exit(-5);
  107.         }
  108.         if (fread(buf, h.ras_maplength, 1, in) != 1) {
  109.             fprintf(stderr, "%s: Read error on colormap.\n",
  110.                 argv[0]);
  111.             exit(-6);
  112.         }
  113.         mapsize = 1<<h.ras_depth; 
  114.         if (h.ras_maplength > mapsize*3) {
  115.             fprintf(stderr,
  116.                 "%s: Huh, %d colormap entries, should be %d?\n",
  117.                 argv[0], h.ras_maplength, mapsize*3);
  118.             exit(-7);
  119.         }
  120.         red = (u_short *)malloc(mapsize * 3 * sizeof (u_short));
  121.         if (red == NULL) {
  122.             fprintf(stderr, "No space for colormap.\n");
  123.             exit(-8);
  124.         }
  125.         for (j = 0; j < 3; j++) {
  126. #define    SCALE(x)    (((x)*((1L<<16)-1))/255)
  127.             for (i = h.ras_maplength/3; i-- > 0;)
  128.                 *red++ = SCALE(*buf++);
  129.             if ((i = h.ras_maplength/3) < mapsize) {
  130.                 i = mapsize - i;
  131.                 bzero(red, i*sizeof (u_short));
  132.                 red += i;
  133.             }
  134.         }
  135.         TIFFSetField(out, TIFFTAG_COLORMAP,
  136.              red, red + mapsize, red + 2*mapsize);
  137.         TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_PALETTE);
  138.         if (compression == (u_short)-1)
  139.             compression = COMPRESSION_PACKBITS;
  140.         TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  141.     } else {
  142.         /* XXX this is bogus... */
  143.         TIFFSetField(out, TIFFTAG_PHOTOMETRIC, h.ras_depth == 24 ?
  144.             PHOTOMETRIC_RGB : PHOTOMETRIC_MINISBLACK);
  145.         if (compression == (u_short)-1)
  146.             compression = COMPRESSION_LZW;
  147.         TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  148.     }
  149.     linebytes = ((h.ras_depth*h.ras_width+15) >> 3) &~ 1;
  150.     if (TIFFScanlineSize(out) > linebytes)
  151.         buf = (u_char *)malloc(linebytes);
  152.     else
  153.         buf = (u_char *)malloc(TIFFScanlineSize(out));
  154.     if (rowsperstrip == (u_long)-1)
  155.         rowsperstrip = (8*1024)/linebytes;
  156.     TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  157.         rowsperstrip == 0 ? 1L : rowsperstrip);
  158.     for (row = 0; row < h.ras_height; row++) {
  159.         if (fread(buf, linebytes, 1, in) != 1) {
  160.             fprintf(stderr, "%s: scanline %d: Read error.\n",
  161.                 argv[0], row);
  162.             break;
  163.         }
  164.         if (TIFFWriteScanline(out, buf, row, 0) < 0)
  165.             break;
  166.     }
  167.     (void) TIFFClose(out);
  168. }
  169.  
  170. usage()
  171. {
  172.     fprintf(stderr, "usage: ras2tif [options] input output\n");
  173.     fprintf(stderr, "where options are:\n");
  174.     fprintf(stderr,
  175.         " -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
  176.     fprintf(stderr,
  177.         " -packbits\tcompress output with packbits encoding\n");
  178.     fprintf(stderr,
  179.         " -none\t\tuse no compression algorithm on output\n");
  180.     fprintf(stderr, "\n");
  181.     fprintf(stderr,
  182.         " -rowsperstrip #\tmake each strip have no more than # rows\n");
  183.     exit(-1);
  184. }
  185.